package cn.cicoding.controller;

import cn.cicoding.entity.User;
import cn.cicoding.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserRpositoryController {

    @Autowired
    IUserService userService;

    @GetMapping("/getAll")
    public List<User> getAllUsers() {
        return userService.findAll();
    }

    @GetMapping("/{userId}")
    public User getByUserId(@PathVariable String userId) {
        return userService.findById(userId);
    }

    @PostMapping("/add")
    public User addNewUser(@RequestBody User user) {
        return userService.save(user);
    }

    @DeleteMapping("/{userId}")
    public String delete(@PathVariable String userId) {
        userService.deleteById(userId);
        return "deleted: " + userId;
    }

    @PutMapping("/update")
    public User update(@RequestBody User user) {
        return userService.save(user);
    }

    @GetMapping("/users")
    public Page<User> getUsers(@RequestParam int page, @RequestParam int size) {
        return userService.findAllUsers(page, size);
    }
}
